using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.Utility;
using OgilvyOne.Model;


namespace OgilvyOne.SqlServerDAL
{
    public class Address : IAddress
    {
        // Methods
        public AddressInfo GetAddressInfo()
        {
            string cmdText = "select top 1 Address_ID,Title,Address_Desc,Create_Date,Last_Modify_Date,Tem_ID,SysUserID from Address_Info order by Address_ID desc";
            AddressInfo info = new AddressInfo();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    info = this.LoadFromDataReader(reader);
                }
            }
            return info;
        }

        private AddressInfo LoadFromDataReader(SqlDataReader reader)
        {
            AddressInfo info = new AddressInfo();
            if (!reader.IsDBNull(0))
            {
                info.Address_id = reader.GetInt32(0);
            }
            if (!reader.IsDBNull(1))
            {
                info.Title = reader.GetString(1);
            }
            if (!reader.IsDBNull(2))
            {
                info.Address_desc = reader.GetString(2);
            }
            if (!reader.IsDBNull(3))
            {
                info.Create_date = reader.GetDateTime(3);
            }
            if (!reader.IsDBNull(4))
            {
                info.Last_modify_date = reader.GetDateTime(4);
            }
            if (!reader.IsDBNull(5))
            {
                info.Tem_id = reader.GetInt32(5);
            }
            if (!reader.IsDBNull(6))
            {
                info.Sysuserid = reader.GetInt32(6);
            }
            return info;
        }

        public void UpdateAddress(AddressInfo mAddressInfo)
        {
            SqlConnection conn = null;
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Address_ID", SqlDbType.Int), new SqlParameter("@Title", SqlDbType.VarChar, 200), new SqlParameter("@Address_Desc", SqlDbType.Text, 0x7fffffff), new SqlParameter("@Create_Date", SqlDbType.DateTime), new SqlParameter("@Last_Modify_Date", SqlDbType.DateTime), new SqlParameter("@Tem_ID", SqlDbType.Int), new SqlParameter("@SysUserID", SqlDbType.Int) };
            cmdParms[0].Value = mAddressInfo.Address_id;
            cmdParms[1].Value = mAddressInfo.Title;
            cmdParms[2].Value = mAddressInfo.Address_desc;
            cmdParms[3].Value = mAddressInfo.Create_date;
            cmdParms[4].Value = mAddressInfo.Last_modify_date;
            cmdParms[5].Value = mAddressInfo.Tem_id;
            cmdParms[6].Value = mAddressInfo.Sysuserid;
            string cmdText = "update Address_Info set Title = @Title,Address_Desc = @Address_Desc,Last_Modify_Date = getDate(),Tem_ID =@Tem_ID,SysUserID =@SysUserID where Address_ID =@Address_ID";
            try
            {
                try
                {
                    conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                    SQLHelper.ExecuteNonQuery(conn, CommandType.Text, cmdText, cmdParms);
                }
                catch (Exception exception)
                {
                    throw new Exception("UpdateAddress", exception);
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}